package com.business.user;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.user.UserInfo;
import com.sql.DBFactory;

public class UserInfoBus {

	private static final Log log = LogFactory.getLog("UserInfoBus.class");

	private Connection conn = null;

	private Statement stmt = null;

	private ResultSet rs = null;

	public void add(UserInfo ui) {
		String userName = ui.getUsername();
		String password = ui.getPassword();

		String nickName = ui.getUserNickname();
		String email = ui.getUserEmail();
		String qq = ui.getUserqq();
		String msn = ui.getMsn();
		String telephone = ui.getTelephone();
		String mobile = ui.getMobile();
		String address = ui.getAddress();
		String province = ui.getProvince();
		String city = ui.getCity();
		String sex = ui.getSex();
		String birthday = ui.getBirthday();
		String homepage = ui.getUserHomepage();
		String gonggao = ui.getGonggao();
		String blogName = ui.getBlogName();
		String ask = ui.getAsk();
		String answer = ui.getAnswer();
		String lastLoginTime = ui.getLastloginTime();
		String loginCount = ui.getLoginCount();
		String isOpen = ui.getIsOpen();
		String regTime = ui.getRegTime();
		String blogMemo = ui.getBlogMemo();
		String lastIp = ui.getUserLastIp();
		String isHide = ui.getIsHide();
		String userIntro = ui.getUserIntro();
		String userLove = ui.getUserLove();
		String userPhotourl = ui.getUserPhotourl();
		String sql = "insert into user_info(username,password,user_nickname,user_Email,userqq,MSN,telephone,mobile,";
		sql += "address,province,city,reg_time,isOpen,sex,birthday,user_homepage,gonggao,user_lastIP,blog_name";
		sql += ",lastlogin_time,login_count,ask,answer,blog_memo,isHide,user_intro,user_love,user_photourl)";
		sql += "values('" + userName + "','" + password + "','" + nickName
				+ "','" + email + "','" + qq + "','" + msn + "','" + telephone
				+ "','" + mobile + "','";
		sql += address + "','" + province + "','" + city + "','" + regTime
				+ "'," + isOpen + ",'" + sex + "','" + birthday + "','"
				+ homepage + "','" + gonggao + "','";
		sql += lastIp + "','" + blogName + "','" + lastLoginTime + "',"
				+ loginCount + ",'" + ask + "','" + answer + "','" + blogMemo
				+ "'," + isHide + ",'" + userIntro + "','" + userLove + "','"
				+ userPhotourl + "')";

		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the insert loginfo exception:" + e.getMessage());
			log.error("the insert loginfo sql is: " + sql);
		}

		String sql_login = "insert into user_login(username,password) values('"
				+ userName + "','" + password + "')";
		try {
			stmt.executeUpdate(sql_login);
		} catch (Exception e) {
			log.error("the insert userlogin exception:" + e.getMessage());
			log.error("the insert userlogin sql is: " + sql_login);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
	}

	public boolean checkLogin(String userName, String password) {
		String sql = "select * from user_login where username='" + userName
				+ "' and password='" + password + "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				return true;
			} else
				return false;
		} catch (Exception e) {
			log.error("the insert loginfo exception:" + e.getMessage());
			log.error("the insert loginfo sql is: " + sql);
			return false;
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
	}

	public boolean checkByStringItem(String keyword, String value) {
		String sql = "select * from user_info where " + keyword + "='" + value
				+ "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				return true;

			} else
				return false;
		} catch (Exception e) {
			log.error("the insert loginfo exception:" + e.getMessage());
			log.error("the insert loginfo sql is: " + sql);
			return false;
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void update(UserInfo ui, String username) {
		String password = ui.getPassword();
		String nickName = ui.getUserNickname();
		String email = ui.getUserEmail();
		String qq = ui.getUserqq();
		String msn = ui.getMsn();
		String telephone = ui.getTelephone();
		String mobile = ui.getMobile();
		String address = ui.getAddress();
		String province = ui.getProvince();
		String city = ui.getCity();
		String sex = ui.getSex();
		String birthday = ui.getBirthday();
		String homepage = ui.getUserHomepage();
		String gonggao = ui.getGonggao();
		String blogName = ui.getBlogName();
		String ask = ui.getAsk();
		String answer = ui.getAnswer();
		String lastLoginTime = ui.getLastloginTime();
		String isOpen = ui.getIsOpen();
		String blogMemo = ui.getBlogMemo();
		String lastIp = ui.getUserLastIp();
		String isHide = ui.getIsHide();
		String userIntro = ui.getUserIntro();
		String userLove = ui.getUserLove();
		String userPhotourl = ui.getUserPhotourl();
		String sql = "UPDATE `user_info` SET";
		if (password != null && !password.equals("")) {
			sql += "`password`='" + password + "',";
		}
		sql += "`user_nickname`='" + nickName + "', `user_Email`='" + email
				+ "', `userqq`='" + qq + "', `MSN`='" + msn
				+ "', `telephone`='" + telephone + "', `mobile`='" + mobile
				+ "', `address`='" + address + "', `province`='" + province
				+ "', `city`='" + city + "',`isOpen`=" + isOpen + ", `sex`='"
				+ sex + "', `birthday`='" + birthday + "', `user_homepage`='"
				+ homepage + "', `gonggao`='" + gonggao + "', `user_lastIP`='"
				+ lastIp + "', `blog_name`='" + blogName + "', `blog_memo`='"
				+ blogMemo + "', `lastlogin_time`='" + lastLoginTime
				+ "',`ask`='" + ask
				+ "', `answer`='" + answer + "',ishide=" + isHide
				+ ",user_intro='" + userIntro + "',user_love='" + userLove
				+ "',user_photourl='" + userPhotourl + "' WHERE `username`='"
				+ username + "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			//System.out.println(sql);
		} catch (Exception e) {
			log.error("the update loginfo exception:" + e.getMessage());
			log.error("the update loginfo sql is: " + sql);

		}
		if (password != null && !password.equals("")) {
			String sql_login = "update  user_login set password='" + password
					+ "' where username='" + username + "'";
			try {
				stmt.executeUpdate(sql_login);
			} catch (Exception e) {
				log.error("the update userlogin exception:" + e.getMessage());
				log.error("the update userlogin sql is: " + sql_login);
			}
		}

		try {
			stmt.close();
			conn.close();
		} catch (SQLException sqle) {
			log.error(sqle.toString());
		}
	}

	public String checkAdminLogin(String userName, String password) {
		String sql = "select * from admin where admin_name='" + userName
				+ "'  and admin_password='" + password + "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				Date date = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				int count = rs.getInt("login_count");
				String group = rs.getString("group_no");
				String time = sdf.format(date);
				count++;
				sql = "update admin set last_login_time='" + time
						+ "', login_count=" + count + " where admin_name='"
						+ userName + "'";
				stmt.executeUpdate(sql);

				return group;

			} else
				return "";
		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
			return "";
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
	}

	public void del(String id) {
		// TODO Auto-generated method stub
		
	}
}
